package com.example.todolist.controller;

import com.example.todolist.model.dto.TodoItemDto;
import com.example.todolist.service.TodoService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/todos")
@RequiredArgsConstructor
public class TodoController {
    private final TodoService todoService;

    @GetMapping
    public ResponseEntity<List<TodoItemDto>> getAllTodos() {
        return ResponseEntity.ok(todoService.getAllTodoItems());
    }

    @GetMapping("/completed")
    public ResponseEntity<List<TodoItemDto>> getCompletedTodos() {
        return ResponseEntity.ok(todoService.getCompletedItems());
    }

    @GetMapping("/incomplete")
    public ResponseEntity<List<TodoItemDto>> getIncompleteTodos() {
        return ResponseEntity.ok(todoService.getIncompleteItems());
    }

    @GetMapping("/{id}")
    public ResponseEntity<TodoItemDto> getTodoById(@PathVariable Long id) {
        return ResponseEntity.ok(todoService.getTodoItemById(id));
    }

    @PostMapping
    public ResponseEntity<TodoItemDto> createTodo(@RequestBody TodoItemDto todoItemDto) {
        return ResponseEntity.ok(todoService.createTodoItem(todoItemDto));
    }

    @PutMapping("/{id}")
    public ResponseEntity<TodoItemDto> updateTodo(@PathVariable Long id, @RequestBody TodoItemDto todoItemDto) {
        return ResponseEntity.ok(todoService.updateTodoItem(id, todoItemDto));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteTodo(@PathVariable Long id) {
        todoService.deleteTodoItem(id);
        return ResponseEntity.noContent().build();
    }

    @PatchMapping("/{id}/toggle")
    public ResponseEntity<TodoItemDto> toggleTodoCompletion(@PathVariable Long id) {
        return ResponseEntity.ok(todoService.toggleTodoItemCompletion(id));
    }
}